//
//  JZChatViewController.m
//  QuanZiChat
//
//  Created by 张洪林 on 15/11/17.
//  Copyright © 2015年 ZhongHongLin. All rights reserved.
//

#import "JZChatViewController.h"
#import "KxMenu.h"
#import "JZAddFriendViewController.h"
#import "JZCreateGroupViewController.h"
#import "QRCodeGenerator.h"
#import <ZBarSDK.h>
#import "TalkManage.h"
#import "TalkingTableViewCell.h"
#import "Person.h"
#import "TalkingViewController.h"
#import <AVOSCloudIM.h>
@interface JZChatViewController ()<ZBarReaderDelegate,UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,strong)NSArray *FriendArray;
@property (weak, nonatomic) IBOutlet UITableView *tableview;

@end

@implementation JZChatViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    
    self.FriendArray = [NSArray array];


    
    self.view.backgroundColor=[UIColor greenColor];
 

    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"找你玩" style:(UIBarButtonItemStylePlain) target:self action:@selector(clickBarButtonAction:)];

}


-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = NO;
    //获取自己的objectID
    //调用coredata查询的方法
    AVUser *currentUser = [AVUser currentUser];
    NSString * MyClientID = currentUser.objectId;
    self.FriendArray = [[TalkManage defulManage] fetchTalkWithMyID:MyClientID];
    [self.tableview reloadData];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.FriendArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    TalkingTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.person = self.FriendArray[indexPath.row];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    TalkingViewController *sendmessage = [self.storyboard instantiateViewControllerWithIdentifier:@"TalkingViewController"];
    Person *person = self.FriendArray[indexPath.row];
    sendmessage.OtherClientID = person.userID;
    sendmessage.username = person.username;
    [self.navigationController pushViewController:sendmessage animated:YES];
}


#pragma mark =========================== 右上角button功能

//左侧按钮点击方法，打开关闭抽屉


-(void)clickBarButtonAction:(UIBarButtonItem *)barButtonItem{
    
    [self.mm_drawerController toggleDrawerSide:MMDrawerSideLeft animated:YES completion:nil];
}
- (IBAction)didClickAddButton:(id)sender {
    NSArray * menuArray = @[[KxMenuItem menuItem:@"添加好友"
                                           image:nil//[UIImage imageNamed:@"action_icon"]
                                          target:self
                                          action:@selector(pushToAddFriend:)],
                            
                            [KxMenuItem menuItem:@"扫一扫"
                                           image:nil//[UIImage imageNamed:@"check_icon"]
                                          target:self
                                          action:@selector(pushToScansion:)],
                            
                            [KxMenuItem menuItem:@"创建群组"
                                           image:nil//[UIImage imageNamed:@"reload"]
                                          target:self
                                          action:@selector(pushToCreation:)]];
    CGRect frame =CGRectMake(self.view.frame.size.width - 50, 20, 45, 50);
    [KxMenu showMenuInView:self.view
                  fromRect:frame
                 menuItems:menuArray];
}

-(void)pushToAddFriend:(id)sender{
    JZAddFriendViewController *addFriendVC = [self.storyboard instantiateViewControllerWithIdentifier:@"JZAddFriendViewController"];
    [self.navigationController pushViewController:addFriendVC animated:YES];
}
-(void)pushToScansion:(id)sender{
    //创建扫描界面
    ZBarReaderViewController *readVC = [self.storyboard instantiateViewControllerWithIdentifier:@"ZBarReaderViewController"];
    readVC.readerDelegate = self;
    //设置支持方向
    readVC.supportedOrientationsMask = ZBarOrientationMaskAll;
    //设置图片扫描器
    ZBarImageScanner *scanner = readVC.scanner;
    //配置扫描器
    [scanner setSymbology:ZBAR_I25 config:ZBAR_CFG_ENABLE to:0];
    
    [self showViewController:readVC sender:nil];
}
-(void)pushToCreation:(id)sender{
    JZCreateGroupViewController *createGroupVC = [self.storyboard instantiateViewControllerWithIdentifier:@"JZCreateGroupViewController"];
    [self.navigationController pushViewController:createGroupVC animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
